package com.train.course.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.course.dto.CommentPublishDTO;
import com.train.course.service.IVideoCommentService;
import com.train.course.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频评论管理 控制器
 */
@RestController
@RequestMapping("/api/v1")
public class VideoCommentController extends BaseController {

    @Autowired
    private IVideoCommentService videoCommentService;

    /**
     * 发布评论
     */
    @PostMapping("/videos/{courseId}/comments")
    public AjaxResult publishComment(@PathVariable("courseId") Long courseId, @RequestBody CommentPublishDTO dto) {
        Long userId = getUserId();
        boolean result = videoCommentService.publishComment(userId, courseId, dto);
        return result ? success("评论发布成功") : error("评论发布失败");
    }

    /**
     * 查询主评论列表（下滑刷新）
     */
    @GetMapping("/videos/{courseId}/comments")
    public AjaxResult getMainComments(@PathVariable("courseId") Long courseId,
                                     @RequestParam(value = "lastCommentId", defaultValue = "0") Long lastCommentId,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<CommentVO> comments = videoCommentService.getMainComments(courseId, lastCommentId, pageSize);
        return success(comments);
    }

    /**
     * 查询子评论列表（下滑刷新）
     */
    @GetMapping("/comments/{parentId}/replies")
    public AjaxResult getReplies(@PathVariable("parentId") Long parentId,
                                @RequestParam(value = "lastReplyId", defaultValue = "0") Long lastReplyId,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<CommentVO> replies = videoCommentService.getReplies(parentId, lastReplyId, pageSize);
        return success(replies);
    }
}
